﻿using System.Collections.Generic;
using System.Globalization;

namespace SpellChecker
{
    public static class Expander
    {
        public static void ExpandDict(this Dictionary<string,bool> Dictionary)
        {
            Dictionary<string, bool> TempDict = Dictionary;

            foreach (var Entry in TempDict)
            {
                if (Entry.Key.Contains("~"))
                {
                    var WordPossibilities = GetAllWords(Entry.Key);
                    WordPossibilities.ForEach(delegate(string S)
                                                  {
                                                      Dictionary.Add(S, true);
                                                  });
                    Dictionary.Remove(Entry.Key);
                }
            }
        }

        internal static List<string> GetAllWords(string S)
        {
            var PräfixAndSuffix = GetPräfixAndSuffix(S);
            var Words = new List<string>();

            foreach (var Word in PräfixAndSuffix)
            {
                if(Word.StartsWith("~"))
                {
                    Words.Add(PräfixAndSuffix[0] + Word.Replace("~", ""));
                }
                else if (Word.EndsWith("~"))
                {
                    Words.Add(Word.Replace("~","") + PräfixAndSuffix[0].ToLower());
                }
                else
                {
                    Words.Add(Word);
                }
            }

            return Words;
        }

        internal static string[] GetPräfixAndSuffix(string S)
        {
            return S.Split(',');
        }
    }
}
